<template>
  <div class="reset-password-mod">
    <Card>
      <p slot="title">
        <Icon type="ios-help-outline" size=16 class="info-icon"></Icon>重置密码</p>
      <Form :model="formData" ref="setNewPasswordForm" :label-width="120" :rules="rules">
        <FormItem label="手机验证码" prop="actCode">
          <Input v-model="formData.actCode" type="text" placeholder="请输入验证码"></Input>
        </FormItem>
        <FormItem label="新密码" prop="password">
          <Input v-model="formData.password" type="password" placeholder="请输入密码"></Input>
        </FormItem>
        <FormItem label="确认密码" prop="repassword">
          <Input v-model="formData.repassword" type="password" placeholder="请输入确认密码"></Input>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="postPassword" :loading="posting">提交</Button>
        </FormItem>
      </Form>
    </Card>
  </div>
</template>
<script>
import { ajax_post_resetPwd } from '../../api/base_sys/user'
export default {
  name: 'hasCode',
  data () {
    return {
      posting: false,
      formData: {
        username: this.$route.params.username,
        type: this.$route.params.type,
        actCode: '',
        password: '',
        repassword: ''
      },
      rules: {
        actCode: [
          { required: true, message: '请输入收到的验证码', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            message: '为保证安全，密码长度应该大于6位',
            trigger: 'blur'
          }
        ],
        repassword: [
          { required: true, message: '请输入确认密码', trigger: 'blur' },
          {
            min: 6,
            message: '为保证安全，密码长度应该大于6位',
            trigger: 'blur'
          },
          { validator: this.check_password, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 判断密码是否相同
    check_password (rule, value, callback, source, options) {
      if (value != this.formData.password) {
        return callback(new Error('两次输入密码不相同'))
      } else {
        callback()
      }
    },
    // 提交密码数据
    postPassword () {
      const self = this
      this.$refs.setNewPasswordForm.validate(valid => {
        if (valid) {
          // if(this.formData.password!=this.formData.repassword){
          //   this.$Notice.error({
          //       title: "两次密码输入不一致",
          //       desc: "请重新再输",
          //       duration: 2
          //     });
          //     return;
          // }
          this.posting = true
          ajax_post_resetPwd(this.formData).then(res => {
            this.posting = false
            if (res.code == 0) {
              // 正常发送邮件
              this.$Notice.success({
                title: '成功',
                desc: '3秒后即将跳转到登录页',
                duration: 3,
                onClose: function () {
                  self.$router.push({
                    name: 'login'
                  })
                }
              })
            } else {
              // 失败发送邮件
              this.$Notice.error({
                title: '失败',
                desc: res.message,
                duration: 0
              })
            }
          })
        }
      })
    }
  },
  created () {
    document.body.setAttribute('class', '')
  }
}
</script>
<style lang="less">
.info-icon {
  margin-right: 10px;
  color: #2781ee;
}

.reset-password-mod {
  width: 400px;
  height: 250px;
  position: fixed;
  left: 50%;
  top: 50%;
  margin-left: -200px;
  margin-top: -125px;
}
</style>
